package com.panshi.qa.provider.modules.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panshi.qa.api.vo.ItemQueryVo;
import com.panshi.qa.api.vo.QaQuestionFrontdesk;
import com.panshi.qa.provider.modules.domain.*;
import com.panshi.qa.provider.modules.mapper.QaQuestionMapper;
import com.panshi.qa.provider.modules.mapper.QaQuestionUncheckMapper;
import com.panshi.qa.provider.modules.mapper.ThSearchMapper;
import com.panshi.qa.provider.modules.service.QaQuestionService;
import com.panshi.qa.provider.modules.service.QuestionRelevanceService;
import com.panshi.qa.provider.modules.service.ThTypeService;
import com.panshi.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QuestionServiceImpl extends ServiceImpl<QaQuestionMapper, QaQuestion> implements QaQuestionService {
    @Autowired
    QaQuestionUncheckMapper qaQuestionUncheckMapper;

    @Autowired
    ThTypeService thTypeService;

    @Autowired
    QuestionRelevanceService questionRelevanceService;

    @Override
    public QaQuestion selectById(Long id) {
        return this.baseMapper.selectById(id);
    }

    //添加问题
    @Override
    public R insertQaQuestion(QaQuestionFrontdesk qaQuestionFrontdesk) {
        //问题对象
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestionDomain = new com.panshi.qa.provider.modules.domain.QaQuestion();
        BeanUtils.copyProperties(qaQuestionFrontdesk,qaQuestionDomain);
        qaQuestionDomain.setCollectNum(0);
        qaQuestionDomain.setReadNum(0);
        qaQuestionDomain.setAnswerNum(0);
        //将对象加入到问题审核通过表
        int count = this.baseMapper.insert(qaQuestionDomain);
        //将该对象添加到动态表中
        this.baseMapper.insertUserDynamic(qaQuestionDomain);
        //将该对象添加到搜索表中
        ThType thtype = thTypeService.selectById(qaQuestionDomain.getTypeId());
        System.out.println("qaQuestionDomain           对象"+qaQuestionDomain);

        this.baseMapper.insertThSearch(qaQuestionDomain,thtype.getName(),null);



        if(count>0){
            return R.ok().msg("问题信息添加到问题表成功···");
        }
        return R.error().msg("问题信息添加到问题表失败···");
    }

    //修改问题阅读量
    @Override
    public void updateRead(Long id) {
        QaQuestion qaQuestion = this.baseMapper.selectById(id);
        int readNum = qaQuestion.getReadNum()+1;
        //维护问题表
        this.baseMapper.update(qaQuestion,new UpdateWrapper<QaQuestion>().set("read_num",readNum).eq("id",id));
        //维护搜索表
        this.baseMapper.updateThSearchRead(qaQuestion);
    }

    //删除问题
    @Override
    public R deleteQuestion(int id) {
        //删除审核表里的指定问题
        int count = this.baseMapper.deleteById(id);
        //删除动态表里的指定问题
        qaQuestionUncheckMapper.deleteUserDynamic(id,"1");
        //删除收藏表里的指定问题
        this.baseMapper.deleteUserCollect(id);
        //删除搜索表的指定问题
        this.baseMapper.deleteThSearch(id);
        if(count>0){
            return R.ok().msg("删除指定问题成功");
        }
        return R.ok().msg("删除指定问题失败");
    }

    //修改问题
    @Override
    public R updateQuestion(com.panshi.qa.api.vo.QaQuestion qaQuestionData) {
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestion = new com.panshi.qa.provider.modules.domain.QaQuestion();

        BeanUtils.copyProperties(qaQuestionData,qaQuestion);

        this.baseMapper.updateQuestion(qaQuestion);
        if(0==qaQuestion.getState()){
            //修改动态表
            this.baseMapper.updateUserDyncmic(qaQuestion.getId().intValue(),0);
            //删除搜索表
            this.baseMapper.deleteThSearch(qaQuestion.getId().intValue());
        }else{
            //修改动态表
            this.baseMapper.updateUserDyncmic(qaQuestion.getId().intValue(),qaQuestion.getState());
            int value = this.baseMapper.queryThsearchOne(qaQuestion.getId());
            if(value==0){
                //获取指定口腔类型名称
                QaQuestion q =  this.baseMapper.selectById(qaQuestion.getId());
                ThType thType = thTypeService.selectById(q.getTypeId());
                String introduce = this.baseMapper.queryAnswerOne(qaQuestion.getId());
                //添加搜索表
                this.baseMapper.insertThSearch(q,thType.getName(),introduce);
            }

        }

        return R.ok().msg("修改问题成功");
    }
    //查询指定对象返回后台
    @Override
    public Map<String,Object> findQuestionOne(int id) {
        QaQuestionAssociated qaQuestionAssociated = this.baseMapper.findQuestionOne(id);
        //返回给后台调用者
        Map<String,Object> map = new HashMap<>();
        //状态吗
        map.put("code",0);
        //状态信息
        map.put("msg","success");
        //查询的问题对象
        map.put("data",qaQuestionAssociated);
        return map;
    }

    @Override//查询指定问题对象,排行版等返回给前台
    public R queryQuestionDetail(Long id) {


        //修改指定问题的阅读量
        this.updateRead(id);
        //问答口腔类型列表
        List<ThType> thTypeList = thTypeService.selectList();
        //问题对象
        QaQuestionAssociated qaQuestion = this.baseMapper.findQuestionOne(id.intValue());
        //分页对象
        ItemQueryVo itemQueryVo = new ItemQueryVo();
        //当前页
        itemQueryVo.setCurrent(0);
        //页数大小
        itemQueryVo.setPageSize(5);

        //问题对象
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestionDomain = new com.panshi.qa.provider.modules.domain.QaQuestion();
        //问答口腔类型ID
        qaQuestionDomain.setTypeId(qaQuestion.getTypeId());
        //问题对象ID
        qaQuestionDomain.setId(id);
        //相关问题排行榜
        List<QaQuesitonRelevance> qaQuestionRrelevantOrderList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,qaQuestionDomain,"order");
        //问答口腔类型ID
        qaQuestionDomain.setTypeId(null);
        //大家都在问排行榜
        List<QaQuesitonRelevance> qaQuestionOrderList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,qaQuestionDomain,"order");
        //打乱顺序
        Collections.shuffle(qaQuestionRrelevantOrderList);
        Collections.shuffle(qaQuestionOrderList);
        //返回给调用者对象
        QuestionResVO resVO = new QuestionResVO();
        //问题对象
        resVO.setQaQuestionAssociated(qaQuestion);
        //相关问题排行榜
        resVO.setQaQuestionOrderList(qaQuestionRrelevantOrderList);
        //大家都在问排行榜
        resVO.setQaQuestionOrderList1(qaQuestionOrderList);
        //问答口腔类型列表
        resVO.setThTypeList(thTypeList);
        return R.ok().data("bigList",  JSONObject.toJSONString(resVO));
    }
    //查询所有指定问答口腔类型下问答列表
    @Override
    public R queryIndexTypeQuestionData(Long typeId) {
        //问题对象
        ItemQueryVo itemQueryVo = new ItemQueryVo();
        //问答口腔类型列表
        List<ThType> thTypeList = thTypeService.selectList();
        //将前台传过来的指定口腔类型ID存储到问题对象中
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestionDomain = new com.panshi.qa.provider.modules.domain.QaQuestion();

        qaQuestionDomain.setTypeId(typeId);

        //查询指定问答口腔类型列表
        List<QaQuesitonRelevance> questionList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,qaQuestionDomain,null);
        //查询按阅读量查询大家都在问
        List<QaQuesitonRelevance> orderList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,qaQuestionDomain,"order");

        //返回调用者对象
        QuestionResVO resVO = new QuestionResVO();
        //根据阅读量倒叙查询并打乱
        resVO.setOrderList(orderList);
        //问答列表
        resVO.setQuestionList(questionList);
        //问答口腔类型列表
        resVO.setThTypeList(thTypeList);
        return R.ok().data("bigList",  JSONObject.toJSONString(resVO)).msg("查询指定口腔类型问答列表成功....");
    }

    //查询所有问答列表
    @Override
    public R queryIndexQuestionData() {
        //问题对象
        ItemQueryVo itemQueryVo = new ItemQueryVo();
        //问答口腔类型列表
        List<ThType> thTypeList = thTypeService.selectList();
        //问答首页列表集合
        List<QaQuesitonRelevance> questionList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,null,null);
        //大家都在问
        List<QaQuesitonRelevance> orderList = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,null,"order");
        //打乱集合
        Collections.shuffle(questionList);
        Collections.shuffle(orderList);

        QuestionResVO resVO = new QuestionResVO();
        //根据阅读量查询前十
        resVO.setOrderList(orderList);
        //问答列表
        resVO.setQuestionList(questionList);
        //问答口腔类型列表
        resVO.setThTypeList(thTypeList);

        return R.ok().data("bigList",  JSONObject.toJSONString(resVO));
    }

    @Override
    public R queryIndexItemPage(ItemQueryVo itemQueryVo) {
        //问题对象
        com.panshi.qa.provider.modules.domain.QaQuestion qaQuestionData = new com.panshi.qa.provider.modules.domain.QaQuestion();
        //type==15代表已答  type==20代表未答
        if(itemQueryVo.getType()!= null && 15==itemQueryVo.getType()){
            qaQuestionData.setAnswerNum(15);
        }else if(itemQueryVo.getType()!= null && 20==itemQueryVo.getType()){
            qaQuestionData.setAnswerNum(20);
        }
        //前台传过来的问答口腔类型ID不为null就存储到问题对象中
        if(itemQueryVo.getCateId()!=null && itemQueryVo.getCateId()!=0){
            qaQuestionData.setTypeId(itemQueryVo.getCateId());
        }
        //返回查询问题关来表数据
        List<QaQuesitonRelevance> list = questionRelevanceService.findQuestionRelevanceList(itemQueryVo,qaQuestionData,null);
        //返回查询问题关来表数据的总记录数
        Integer total = questionRelevanceService.findQuestionRelevanceListTotal(qaQuestionData);
        //返回调用者对象
        QuestionResVO resVO = new QuestionResVO();
        //问答列表存储到返回对象
        resVO.setQuestionList(list);
        //记录数存储到返回对象
        return R.ok().data("bigList",  JSONObject.toJSONString(resVO)).data("total",total);
    }
}
